<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="退料单编码" prop="returnNo">
        <el-input v-model="queryParams.returnNo" placeholder="请输入退料单编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商编码" prop="supplierCode">
        <el-input v-model="queryParams.supplierCode" placeholder="请输入供应商编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="存货编码" prop="inventoryCode">
        <el-input v-model="queryParams.inventoryCode" placeholder="请输入存货编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:return:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:return:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:return:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:return:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="returnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="60" align="center" :index="computeIndex" />
      <el-table-column label="退料单编码" align="center" prop="returnNo" width="160" />
      <el-table-column label="供应商编码" align="center" prop="supplierCode" width="140" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" width="180" />
      <el-table-column label="存货编码" align="center" prop="inventoryCode" width="140" />
      <el-table-column label="存货名称" align="center" prop="inventoryName" width="180" />
      <el-table-column label="批次号" align="center" prop="batchNo" width="140" />
      <el-table-column label="退料数量" align="center" prop="returnQuantity" width="120" />
      <el-table-column label="计量单位名称" align="center" prop="unitName" width="120" />
      <el-table-column label="来源箱码" align="center" prop="barcodeId" width="120" />
      <el-table-column label="收料通知单" align="center" prop="receiptNoteNo" width="160" />
      <el-table-column label="检验计划编码" align="center" prop="inspectionPlanNo" width="160" />
      <el-table-column label="状态" align="center" prop="status" width="120" />
      <el-table-column label="备注" align="center" prop="remark" width="220" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:return:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:return:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="退料单编码" prop="returnNo">
          <el-input v-model="form.returnNo" placeholder="留空自动生成" />
        </el-form-item>
        <el-form-item label="供应商编码" prop="supplierCode">
          <el-input v-model="form.supplierCode" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" />
        </el-form-item>
        <el-form-item label="存货编码" prop="inventoryCode">
          <el-input v-model="form.inventoryCode" />
        </el-form-item>
        <el-form-item label="存货名称" prop="inventoryName">
          <el-input v-model="form.inventoryName" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="form.batchNo" />
        </el-form-item>
        <el-form-item label="退料数量" prop="returnQuantity">
          <el-input-number v-model="form.returnQuantity" :min="0" :controls="true" style="width: 100%" />
        </el-form-item>
        <el-form-item label="计量单位编码" prop="unitCode">
          <el-input v-model="form.unitCode" />
        </el-form-item>
        <el-form-item label="计量单位名称" prop="unitName">
          <el-input v-model="form.unitName" />
        </el-form-item>
        <el-form-item label="退料原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" />
        </el-form-item>
        <el-form-item label="来源箱码ID" prop="barcodeId">
          <el-input v-model="form.barcodeId" />
        </el-form-item>
        <el-form-item label="收料通知单编码" prop="receiptNoteNo">
          <el-input v-model="form.receiptNoteNo" />
        </el-form-item>
        <el-form-item label="检验计划编码" prop="inspectionPlanNo">
          <el-input v-model="form.inspectionPlanNo" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="草稿" value="DRAFT" />
            <el-option label="已确认" value="CONFIRMED" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  </template>

<script>
import { listReturn, getReturn, addReturn, updateReturn, delReturn } from '@/api/system/return'

export default {
  name: 'PurchaseReturn',
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      returnList: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnNo: null,
        supplierCode: null,
        inventoryCode: null,
        batchNo: null
      },
      form: {},
      rules: {
        returnQuantity: [ { required: true, message: '退料数量不能为空', trigger: 'blur' } ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listReturn(this.queryParams).then(resp => {
        this.returnList = Array.isArray(resp.rows) ? resp.rows : []
        this.total = resp.total || this.returnList.length
        this.loading = false
      })
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.returnId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() { this.reset(); this.open = true; this.title = '新增采购退料单' },
    handleUpdate(row) {
      this.reset()
      const id = row.returnId || this.ids
      getReturn(id).then(resp => { this.form = resp.data; this.open = true; this.title = '修改采购退料单' })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        if (this.form.returnId != null) {
          updateReturn(this.form).then(() => { this.$modal.msgSuccess('修改成功'); this.open = false; this.getList() })
        } else {
          addReturn(this.form).then(() => { this.$modal.msgSuccess('新增成功'); this.open = false; this.getList() })
        }
      })
    },
    cancel() { this.open = false; this.reset() },
    reset() {
      this.form = {
        returnId: null,
        returnNo: null,
        supplierCode: null,
        supplierName: null,
        inventoryCode: null,
        inventoryName: null,
        batchNo: null,
        returnQuantity: null,
        unitCode: null,
        unitName: null,
        reason: null,
        barcodeId: null,
        receiptNoteNo: null,
        inspectionPlanNo: null,
        status: 'DRAFT',
        remark: null
      }
      this.resetForm('form')
    },
    handleDelete(row) {
      const ids = row.returnId || this.ids
      this.$modal.confirm('是否确认删除退料单编号为"' + ids + '"的数据项？').then(() => delReturn(ids)).then(() => {
        this.getList(); this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    handleExport() {
      this.download('system/return/export', { ...this.queryParams }, `purchase_return_${new Date().getTime()}.xlsx`)
    },
    computeIndex(index) { return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1 }
  }
}
</script>

<style scoped>
</style>